/**
 * Created by ian0214 on 2017/7/19.
 */
function Controller(options) {
    var opt = options;
    var self = this;
    var editor = new Editor({
        containerId: "edit"
    });

    var preview = $("#preview");

    this.init = function () {
        var handler = function (e) {
            // console.log(e.target);
            self.render(editor.getPureText());
        };

        $("#edit")//.on("DOMNodeInserted", handler)
            .on("keyup", handler)
            .on("paste", function (e) {
                if(opt.pastePlainText){
                    // cancel original paste
                    e.preventDefault();
                    console.log(e);
                    var plainText = e.originalEvent.clipboardData.getData("text/plain");
                    plainText = plainText.replace(/\n/g, "<br />");
                    console.log(plainText);
                    document.execCommand("insertHTML", false, plainText);
                }else{
                    handler(e);
                }

            })
            .on("cut", handler);
    };

    this.render = function (rawMD) {
        var raw = '';
        if(arguments.length === 0){
            raw = editor.getPureText();
        }else{
            raw = rawMD;
        }
        var html = marked(raw);
        preview.html(html);
    }
}